<?
///////////////////////////////////////////
// 				Helper classes
///////////////////////////////////////////

define('MAL_SUCCESS',0);
define('MAL_EMPTY_RESULT',1);
define('MAL_DB_ERROR',2);

class MAL extends DAL
{
	protected $host		= '';
	protected $user		= '';
	protected $password	= '';
	protected $database	= '';
	protected $link=null;
	
	public $row_count = -1;
	public $hasError = false;
	public $errorMsg = '';  

	
	public $rows;
	
	// used to store sqls
	public $sql_list;
	//public $sql_list_param_types;
	
	function __construct($host='',$user='',$password='-1',$database='')
	{		
		/*
		 *  instead of using app specific logic here, should be used MYMAL 
		 *  to be extended by models like PostModel
		 */
		//$zConfig->database->host,$zConfig->database->username,$zConfig->database->password,$zConfig->database->dbname
		if(empty($host)){
			$host = zConfig('database/host');
		}
		if(empty($user)){
			$user = zConfig('database/username');
		}
		if($password=='-1'){
			$password = zConfig('database/password');
		}
		if(empty($database)){
			$database =  zConfig('database/dbname');;
		}
		
		$this->host = $host;
		$this->user = $user;
		$this->password = $password;
		$this->database = $database;
		//echo "$host,$password,$database,$user";
		
		$this->link = @mysql_connect($this->host,$this->user,$this->password);
		//if(mysql_error($this->link)){
		//	zDie('Failed to connect to database');
		//} 
		//echo 'user = '.$zConfig->database->username.'<hr>';
		mysql_select_db($this->database,$this->link);
		//die($database);
		mysql_query("SET character_set_results=utf8",$this->link);
		$this->row_count = -1;
		
		
		$this->sql_list = array();
		//$this->sql_list_param_types = array();
	}
	
	function storeSql($key,$sql){
		$this->sql_list[$key] = $sql;
		//$this->sql_list_param_types[$key] = $paramtypes;
	}
	
	function runSql($key,$params=array()){
		$sql = $this->sql_list[$key];
		$res = $this->execute($sql,$params);
		if($res === MAL_DB_ERROR)return MAL_DB_ERROR;
		
		@$this->row_count = mysql_num_rows($res);
		if($this->row_count<1){
			return MAL_EMPTY_RESULT;
		}
		
		$this->rows = array();
		while($row = mysql_fetch_assoc($res)){
			$this->rows[] = $row;
		}
		return MAL_SUCCESS;
	}
	
	function executeSql($key, $params = array()){
		$sql = $this->sql_list[$key];
		$res = $this->execute($sql,$params);
		if($res === MAL_DB_ERROR)return MAL_DB_ERROR;
		
		@$this->row_count = mysql_affected_rows($this->link);
		
		if($this->row_count < 1) return MAL_EMPTY_RESULT;
		
		return MAL_SUCCESS;
		
	}
	function execute($sql, array $params = array())
	{
		if( count($params) > 0 ){
			$sql = vsprintf( $sql, $params );
		}
		//echo ($sql);
		//$this->link = mysql_connect($this->host,$this->user,$this->password); 
		mysql_select_db($this->database);	
		//mysql_select_db($this->database,$this->link);
//		echo($sql);die();
		mysql_select_db($this->database,$this->link);
		//mysql_query("SET character_set_results=utf8",$this->link);
		mysql_query("SET character_set_results=utf8");
		mysql_set_charset('utf8');
		//die($sql);
		$res =  mysql_query($sql);
		//echo $sql;
		@$this->hasError = mysql_errno($this->link);
		if(empty($this->hasError)){
			$this->hasError =false;
		}else{
			$this->hasError =true;
			$this->errorMsg = mysql_error($this->link);
			return MAL_DB_ERROR;
			//echo(mysql_error($this->link)); 
		}
                
		//var_dump($this->hasError);
		//echo mysql_error($this->link);
		//die('hre='.$this->row_count);
                
		return $res;
	}
	
	
	public function getRowCount(){
		return $this->row_count;
	}
	
	function get_table_columns($table){}
	
	function get_connection()
	{
		return $this->link;
	}
	
	/**
	 * Checks if the value is unique in the user table
	 *
	 * @param  $col The column in the user tabls
	 * @param  $val The value to be checked
	 */
	public function isUnique( $col, $val, $table ){
		$col = mysql_real_escape_string($col);
		$val = mysql_real_escape_string($val);
		$table = mysql_real_escape_string($table);
		$sql = "select * from $table where $col='$val';";
		$res = $this->execute($sql);
		if($this->row_count>0){
			return false;
		}
		return true;
	}
	
	function update($table,$id,$data){
		$vals = '';
		foreach($data as $key=>$val){
			$vals .= "$key=$val,";
		}
		$vals = substr($vals,0,-1);
		$sql = "update $table set $vals where id$table=$id";
		$res = $this->execute($sql);
	}
	
	function addNew($table,$data){
		$vals = '';
		$cols = '';
		foreach($data as $key=>$val){
			$vals .= "$val,";
			$cols .= "$key,";
		}
		$vals = substr($vals,0,-1);
		$cols = substr($cols,0,-1);
		
		$sql = "insert into $table($cols) values($vals)";
		
		$res = $this->execute($sql);
		if($this->hasError)return false;
		
		return mysql_insert_id($this->link);
	}
	function getInsertId(){
		return mysql_insert_id($this->link);
	}
	
	function clean($val){
		$val = mysql_real_escape_string($val,$this->link);
		return $val;
	}
	//TODO simple prepared statement
	/*
	 function preparedStatement(){}
	 //*/
}
?>
